'''
Created on 05.06.2011

@author: mutius
'''
import MySQLdb.cursors

class BFCursor(MySQLdb.cursors.Cursor):
    def sendRequest(self, command, prepareList=False):
        self.execute(command)
        data = self.fetchall()
        if prepareList == True:
            data = map(lambda x: x[0].decode('utf-8'), data)
        return data
    
    def getOnlyValues(self, anylist):
        return map(lambda x: x[0].decode('utf-8'), anylist)
    
    def getColumnLabels(self, table=None):
        if table != None:
            self.execGetAllFrom(table)
        return map(lambda x: x[0], self.description)
    
    def execGetDatabases(self):
        return self.sendRequest("show databases;", True)
    
    def execSelectDb(self, db):
        self.sendRequest("use "+ db)
    
    def execGetTables(self, db=None):
        if db!=None:
            self.sendRequest("use "+ db)
        return self.sendRequest('show tables;', True)
    
    def execGetAllFrom(self, table):
        return self.sendRequest('select * from '+table)
        
    #def getColumnLabels(self, table):
    #    self.sendRequest("")
    
        